import moment from 'moment';

export default {
  data() {
    return {
      globalCount: 0
    }
  },
  methods: {
    /**
     * 百分比格式化
     */
    percentFormat(value) {
      if (value == null) return '-';
      if (value === 'N/A') return value;
      if (isNaN(value)) return 'N/A';

      return `${value}%`;
    },
    /**
     * 百分比特殊格式化
     */
    percentSpecialFormat(originValue) {
      if (originValue == null) return '-';
      if (originValue === 'N/A') return originValue;
      if (isNaN(originValue)) return 'N/A';

      let value = Number(originValue);

      if (!value) {
        value = 0;
      }

      value = parseFloat(value.toFixed(3));

      const [integer, decimal] = value.toFixed(3).split('.');

      return `${integer}<i>.${decimal}%<i>`;
    },
    /**
     * 人民币格式化
     */
    cnyFormat(originValue) {
      let value = Number(originValue);

      if (!value) {
        value = 0;
      }

      return `¥${value.toFixed(2)}`;
    },
    /**
     * 人民币 ,格式化
     */
    rmbFormat(originValue) {
      if (!originValue) return '0.00';
      let str;
      let source = (originValue + '').split('.');
      if (!originValue.length) {
        str = '0.00';
      }
      if (source.length < 2 && source.length > 0) {
        str = this.formattedNumber(originValue) + '.00';
      } else if (source.length === 2) {
        let tt = this.formattedNumber(source[0]);
        if (source[1].length === 1) {
          str = tt + `.${source[1]}0`;
        } else if (source[1].length === 2) {
          str = tt + `.${source[1]}`;
        } else if (source[1].length > 2) {
          str = tt + `.${source[1].substr(0, 2)}`
        }
      }
      return str;
    },
    // 金额数字 三位截取
    formattedNumber(num) {
      var num = (num || 0).toString();
      var result = '';
      while (num.length > 3) {
        result = ',' + num.slice(-3) + result;
        num = num.slice(0, num.length - 3);
      }
      if (num) {
        result = num + result;
      }
      return result;
    },
    /**
     * 日期格式化
     */
    dateFormat(date) {
      if (!date) return;
      return moment(date).format('YYYY-MM-DD').substr(0, 10);
    },
    nullFormat(value) {
      if (value == null) return '-';

      return value;
    },
    /** 
     * 银行卡号格式化 4原则
     */
    cardFormat(card) {
      if (!card) return '';
      return card && card.replace(/\D/g, '').replace(/(....)(?=.)/g, '$1 ');
    },
    /** 
     * 银行卡号格式化 4 * 原则
     * 16，17，18，19
     */
    newCardFormat(card) {
      if (!card) return;
      let size = card.length;
      let newArr;
      switch (size) {
        case 16:
          newArr = `**** **** **** ${card.substring(12)}`;
          break;
        case 17:
          newArr = `**** **** **** *${card.substring(13, 16)} ${card.substring(16)}`;
          break;
        case 18:
          newArr = `**** **** **** **${card.substring(14, 16)} ${card.substring(16)}`;
          break;
        case 19:
          newArr = `**** **** **** ***${card.substring(15, 16)} ${card.substring(16)}`;
          break;
        default:
          break;
      }
      return newArr;
    },
    /** 
     * 手机格式化 334原则
     */
    phoneFormat(phone) {
      if (!phone) return;
      return phone && phone.replace(/\B(?=(?:\d{4})+$)/g, ' ');
    },
    // 隐藏身份中中间几位
    visibleIdentityCard(cardNo) {
      let newCardNo;
      let size = cardNo && cardNo.length;
      switch (size) {
        case 15:
          newCardNo = cardNo.substr(0, 6) + '*****' + cardNo.substr(11);
          break;
        case 18:
          newCardNo = cardNo.substr(0, 6) + '********' + cardNo.substr(14);
          break;
        default:
          break;
      }
      return newCardNo;
    },
    // 日期格式化
    formatDate(date) {
      if (!date) return;
      let newDate = date.substr(0, 10).split('-');
      let aDate = newDate[0] + '年' + newDate[1] + '月' + newDate[2] + '日';
      return aDate;
    },
    // 手机号码式化 3， 4， 4
    formatPhone(phone) {
      if (!phone || typeof phone !== 'string') return '';
      let res = phone.replace(/^(...)(....)/g, "$1 $2 ");
      return res;
    },
    // 身份证号码格式化 3 3 8 4
    formatIdentify(card) {
      if (!card || typeof card !== 'string') return '';
      let res = card.substring(0, 6) + ' ' + card.substring(6, 14) + ' ' + card.substring(14);
      return res;
    },
    /**
     * 创建数据缓存
     */
    createCache() {
      let cache = {};
      return function (key, value) {
        //如果传了值，就说名是设置值
        if(value !== undefined){
          cache[key] = value;
          return cache[key];
        } else { //如果没有传值，只穿了键，那就是获取值
          return cache[key];
        }
      }
    },
    createFib(){
      let fibCache = this.createCache();
      function fib(n){
          this.globalCount++;
          //1.从cache中获取数据
          if(fibCache(n) !== undefined){
              //如果缓存中有 直接返回
              return fibCache(n) ;
          }
          //如果缓存中没有 就计算
          if(n <= 2){
              //把计算结果存入数组
              fibCache(n , 1) ;
              return 1;
          }
          var temp = fib(n - 1) + fib(n - 2);
          //把计算结果存入数组
          fibCache(n, temp) ;
          return temp;
      }
      return fib;
    }
  }
}
